package com.jlastu.znb.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jlastu.znb.R;
import com.jlastu.znb.entity.Goods;
import com.jlastu.znb.entity.News;
import com.jlastu.znb.entity.NewsPhoto;
import com.jlastu.znb.utils.SpUtils;
import com.jlastu.znb.view.DetailsActivity;
import com.jlastu.znb.view.NewsDetailActivity;

import java.util.List;


public class NewsListAdapter extends RecyclerView.Adapter<NewsListAdapter.ViewHolder> {

   private List<News> mList;
   private RecyclerView.ViewHolder holder;
   private Context context;
   private SpUtils mNewsList;
   private NewsListAdapter mRecyclerAdapter;

   public NewsListAdapter(List<News> list){
      mList = list;
   }

   public void updateData(List<News> list) {
      mList.clear();
      mList = list;
      notifyDataSetChanged();
   }

   @NonNull
   @Override
   public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item, null);
      ViewHolder viewHolder = new ViewHolder(view);
      return viewHolder;
   }

   public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
      News news = mList.get(position);
      holder.newsTitle.setText(news.getTitle());
      holder.newsSubtitle.setText(news.getSubtitle());
      holder.newsDate.setText(news.getDate());
      holder.itemView.setOnClickListener(v -> {
         // 假设你有一个方法来处理新闻点击事件
      });
      
      List<NewsPhoto> photoList = news.getPhotoList();
      if (photoList != null && photoList.size() > 0) {
         byte[] byteArray = photoList.get(0).getData();
         if (byteArray != null && byteArray.length > 0) {
            Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
            holder.photo.setImageBitmap(bitmap);
            return;
         }

      }
      //没有查到图片显示默认图片
      holder.photo.setImageResource(R.mipmap.ic_launcher);
      holder.itemView.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            // 点击列表项时，跳转到 DetailsActivity
            News clickedNews = mList.get(holder.getAdapterPosition());
            Context context = v.getContext();
            Intent intent = new Intent(context, NewsDetailActivity.class);
            // 使用当前点击项的goods_id
            intent.putExtra("GOODS_ID_KEY", clickedNews.getId());
            intent.putExtra("newsDetailUrl", "nong6.html");
            Log.d("GoodsListAdapter", "GOODS_ID_KEY: " + clickedNews.getId());
            context.startActivity(intent);
         }
      });

   }


   @Override
   public int getItemCount() {
      return mList.size();
   }

   public void showNewsByCategory(List<News> newsList) {
      mNewsList.clear();
      mNewsList.addAll(newsList);
      mRecyclerAdapter.updateData((List<News>) mNewsList); // 刷新 RecyclerView 数据
   }
   public class ViewHolder extends RecyclerView.ViewHolder {
      ImageView photo;
      TextView newsTitle;
      TextView newsSubtitle;
      TextView newsDate;

      public ViewHolder(@NonNull View itemView) {
         super(itemView);
         photo = itemView.findViewById(R.id.news_photo);
         newsTitle = itemView.findViewById(R.id.news_title);
         newsSubtitle = itemView.findViewById(R.id.news_subtitle);
         newsDate = itemView.findViewById(R.id.news_date);
      }
   }
}